<?php

/**
 * admincodeformats actions.
 *
 * @package    retailbrain
 * @subpackage admincodeformats
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class admincodeformatsActions extends sfActions {

    /**
     * Executes codeformats action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {

        $this->getResponse()->setSlot('title', 'Administración : Código Formato');
        $this->breadcrumbs = array(array("Administración", "admin/index"),array("Código Formato", "admincodeformats/index"));
        
        $q = Doctrine_Query::create()
                ->from('Codeformats c')
                ->orderBy('c.name desc');
        
        $this->codeformats = $q->fetchArray();
        
        $this->caps = $this->getUser()->getAttribute('caps');
        
    }

    
    /**
     * Executes editCodeformats action
     *
     * @param sfRequest $request A request object
     */
    public function executeEditCodeformats(sfWebRequest $request) {

        $id = $this->getRequestParameter('id');

        $this->getResponse()->setSlot('title', 'Administración : Editar Código Formato');
        $this->breadcrumbs = array(array("Administración", "admin/index"),array("Código Formato", "admincodeformats/index"), array("Editar Código Formato", "admincodeformats/editCodeformats?id=$id"));
        
        
        /** Channel **/
        $q = Doctrine_Query::create()
                ->from('Codeformats c')
                ->where('c.id_codeformat = ?', array($id))
                ->fetchArray();

        $this->codeformat = $q;
        
    }
    
    
    /**
     * Executes addCodeformats action
     *
     * @param sfRequest $request A request object
     */
    public function executeAddCodeformats(sfWebRequest $request) {

        $this->getResponse()->setSlot('title', 'Administración : Agregar Código Formato');
        $this->breadcrumbs = array(array("Administración", "admin/index"),array("Código Formato", "admincodeformats/index"), array("Agregar Código Formato", "admincodeformats/addCodeformats"));
        
    }
    
    
    /**
     * Executes createCodeformat action
     *
     * @param sfRequest $request A request object
     */
    public function executeCreateCodeformat(sfWebRequest $request) {


        // Obtengo Datos del Formulario
        $_arrayPost = $request->getParameter('codeformats');
        
        try {

            $tabla = Doctrine::getTable('CodeFormats');
            $tabla = new CodeFormats();

            $tabla->name        = strtoupper($_arrayPost['name']);
            
            $tabla->save();
        } catch (Exception $e) {
            syslog(LOG_ERR, "Error: " . $e->getMessage());
        }


        $this->redirect('/admincodeformats/index');
    }
    
    
    /**
     * Executes updateCodeformat action
     * 
     * @param sfWebRequest $request
     * @return type 
     */
    public function executeUpdateCodeformat(sfWebRequest $request) {

        // Obtengo Datos del Formulario
        $_arrayPost = $request->getParameter('codeformats');
        
        try {

            $tabla = Doctrine::getTable('CodeFormats')->find(array($_arrayPost["id"]));

            $tabla->name        = strtoupper($_arrayPost['name']);

            $tabla->save();
        } catch (Exception $e) {
            syslog(LOG_ERR, "Error: " . $e->getMessage());
        }

        $this->redirect('/admincodeformats/index');
    }
    
    
    
    /**
     * CodeformatActive
     * [activo:inactivo]
     * @param sfWebRequest $request 
     */
    public function executeUpdateActiveCodeformat(sfWebRequest $request) {

        // ID, Active
        $id = $request->getParameter('id');
        $active = $request->getParameter('active');
        $active = (($active == 0) ? 1 : 0);

        // Actualizo User
        $_data = Doctrine_Query::create()
                ->update('CodeFormats c')
                ->set('c.active', '?', $active)
                ->where('c.id_codeformat = ?', array($id))
                ->execute();

        // all Users
        $q = Doctrine_Query::create()
                ->from('CodeFormats c')
                ->orderBy('c.name desc');
        
        $this->codeformats = $q->fetchArray();

        $this->caps = $this->getUser()->getAttribute('caps');

        $this->setTemplate('lstCodeformats');
    }
    
    /**
     * CodeformatDelete
     * [activo:inactivo]
     * @param sfWebRequest $request 
     */
    public function executeUpdateDeleteCodeformat(sfWebRequest $request) {

        // ID, Active
        $id = $request->getParameter('id');
        $deleted = $request->getParameter('deleted');
        $deleted = (($deleted == 0) ? 1 : 0);

        // Actualizo User
        $_data = Doctrine_Query::create()
                ->update('CodeFormats c')
                ->set('c.deleted', '?', $deleted)
                ->where('c.id_codeformat = ?', array($id))
                ->execute();

        // all Users
        $q = Doctrine_Query::create()
                ->from('CodeFormats c')
                ->orderBy('c.name desc');
        
        $this->codeformats = $q->fetchArray();

        $this->caps = $this->getUser()->getAttribute('caps');

        $this->setTemplate('lstCodeformats');
    }
    
}
